﻿using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using VkClientIM.Logic;
using VkClientIM.UI;

namespace VkClientIM.Windows
{
	/// <summary>
	/// Interaction logic for ConversationWindow.xaml
	/// </summary>
	public partial class ConversationWindow
		: Window,IConversationWindow
	{
		private List<Conversation> _conversations;

		public ConversationWindow()
		{
			InitializeComponent();
		}


		protected override void OnClosing(System.ComponentModel.CancelEventArgs e)
		{
			e.Cancel = true;
			this.Hide();
			base.OnClosing(e);
		}

		public void LoadConversation(Conversation c)
		{
			Dispatcher.Invoke(new Action(
			                  	delegate
			                  		{
			                  			ConversationPresenter p = new ConversationPresenter();
			                  			p.AttachConversation(c);
			                  			conversationHolder.Items.Add(p);
			                  			conversationHolder.SelectedItem = p;
			                  			if (this.Visibility != Visibility.Visible)
			                  			{
			                  				Visibility = System.Windows.Visibility.Visible;
			                  			}
			                  		}));

		}


		public bool IsConversationOpened(Conversation c)
		{
			return (bool) Dispatcher.Invoke(new Func<bool>(
			                                	delegate
			                                		{
			                                			foreach (ConversationPresenter item in conversationHolder.Items)
			                                			{
			                                				Conversation test = item.DataContext as Conversation;
			                                				if (c == test)
			                                				{
			                                					return true;
			                                				}
			                                			}
			                                			return false;
			                                		}));

		}

		public new void Show()
		{
			Dispatcher.Invoke(new Action(IntShow));
		}

		private void IntShow()
		{
			base.Show();
		}
	}

}
